﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
public partial class Register : System.Web.UI.Page
{
    MaHoaMK_BLL MaHoa = new MaHoaMK_BLL();
    KhachHang_BLL KH = new KhachHang_BLL();
    DataTable dt = new DataTable();
    public static string so1, so2, ketqua;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TaoMa();
            txtTenDN.Focus();
        }
    }   
    public void TaoMa()
    {
        string strString = "0123456789";
        Random random = new Random();
        int randomCharIndex = 0;
        char randomChar;
        for (int i = 0; i < 1; i++)
        {
            randomCharIndex = random.Next(0, strString.Length);
            randomChar = strString[randomCharIndex];
            so1 = Convert.ToString(randomChar);
        }
        for (int i = 0; i < 1; i++)
        {
            randomCharIndex = random.Next(0, strString.Length);
            randomChar = strString[randomCharIndex];
            so2 = Convert.ToString(randomChar);
        }
        lblcauhoi.Text = "<font color='red'>" + so1 + "</font> + <font color='red'>" + so2 + "</font> =?";
        ketqua = (int.Parse(so1) + int.Parse(so2)).ToString();
    }
    protected void txtDangKi_Click(object sender, EventArgs e)
    {
        if (ketqua != txtTraLoi.Text)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "RedirectMe", "alert('Câu trả lời không đúng!');", true);
            TaoMa();
        }
        else
        {
            try
            {
                string error = "";
                string MatKhau = MaHoa.GetMD5(txtMatKhau.Text);
                string NS = DropNgay.SelectedValue + "/" + DropThang.SelectedValue + "/" + txtNam.Text;
                DateTime NgaySinh = new DateTime();
                DateTime NgayDangKi = new DateTime();
                NgaySinh = DateTime.ParseExact(NS, "dd/MM/yyyy", null);
                NgayDangKi = DateTime.Now;

                bool isExits = KH.CheckExitsUser(txtEmail.Text, txtTenDN.Text);
                if (isExits)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('UserName or Email had exits, please select different name!');", true);
                    return;
                }
                else
                {

                    KH.AddNewCustomer(1, txtTenDN.Text, MatKhau, txtEmail.Text, txtHoTen.Text, txtDienThoai.Text, txtDiaChi.Text, RadioBtnGioiTinh.SelectedValue, NgaySinh, string.Empty, NgayDangKi, 1,1, ref error);
                    if (error + "" != "")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + error + "');", true);
                    }
                    else
                    {
                        Session["Khach"] = txtTenDN.Text;
                        Response.Redirect("~/RegisterSuccess.aspx");
                    }
                }
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Lỗi! không thực hiện được');", true);
            }           
        }
    }
    protected void txtTuBo_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
}
